﻿using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.MerchantsSettings
{
    public class CanCiController : BaseController
    {
        //餐次定义
        // GET: /CanCi/

        ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
        OrgDC.Service1Client orgdc = new OrgDC.Service1Client();

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取管理员下面的商户
        /// </summary>
        /// <returns></returns>
        public ActionResult getmer()
        {
            try
            {
                Page<Model.OrgOrder_Mer_T> page = new Page<Model.OrgOrder_Mer_T>();
                List<Model.OrgOrder_Mer_T> list = new List<Model.OrgOrder_Mer_T>();//定义商品分类设置
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string result = service.GetMer(user.OperID, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    var a = searcho["rows"].Children().ToJson();
                    return Json(a, JsonRequestBehavior.AllowGet);
                }
                return Json(searcho["Msg"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取商户餐次信息
        /// </summary>
        /// <param name="merid"></param>
        /// <returns></returns>
        public ActionResult GetMealInfo(int merid)
        {
            try
            {
                string result = orgdc.GetMealInfo(merid, YZM, 1);
                List<Model.OrgOrder_Meal_T> list = new List<Model.OrgOrder_Meal_T>();
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    list = ParseClass<Model.OrgOrder_Meal_T>.JsonList(jieguo);//解析返回的结果\
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            
            
        }


        /// <summary>
        /// 更新餐次信息
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="box"></param>
        /// <returns></returns>
        public ActionResult updateMeal(int tag, Model.OrgOrder_Meal_T oom)
        {
            try
            {
                if (tag == 3)//删除
                {
                    oom.MealState = 0;
                }
                string a = oom.ToJson();
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];
                string result = orgdc.UpdateMeal(user.OperID, oom.ToJson(), tag, YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(searcho["Msg"].ToString());
                }
            }
            catch (Exception)
            {
                return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面

            }
        }

       /// <summary>
       /// 获取商户的所有商品
       /// </summary>
       /// <param name="MerID"></param>
       /// <param name="jqgridparam"></param>
       /// <returns></returns>
        public ActionResult getgrid(string MerID, JqGridParam jqgridparam)
        {
            try
            {
                Page<View_Order_Dishes> page = new Page<View_Order_Dishes>();//分页类定义
                List<View_Order_Dishes> list = new List<View_Order_Dishes>();
                list = GetAllDishes(MerID);
                if (list.Count > 0)
                {
                    jqgridparam.records = list.Count;//查询的数据总数
                    var data = new
                    {
                        total = list.Count.ToString(),
                        rows = page.FindListPage(list, ref jqgridparam)
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = "0",
                        rows = list
                    };
                    return Json(data);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 获取对应商户的所有商品
        /// </summary>
        /// <param name="MerID"></param>
        /// <returns></returns>
        public List<View_Order_Dishes> GetAllDishes(string MerID)
        {
            string result = service.GetOrder_Dishes(MerID, "", 1);
            string siteUrl = ConfigurationManager.AppSettings["Photo"];
            JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
            string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
            if (bs == "1")//返回结果正常
            {
                List<View_Order_Dishes> list = new List<View_Order_Dishes>();//用于保存当前商户下的所有菜品集合
                IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                if (jieguo.Count > 0)
                {
                    foreach (JToken item in jieguo)
                    {
                        List<View_Order_Dishes> list_Dishes = new List<View_Order_Dishes>();
                        View_Order_Dishes m = new View_Order_Dishes();
                        m.DishesID = item["DishesID"].ToString();
                        m.DishesNmae = item["DishesNmae"].ToString();
                        m.MerID = int.Parse(item["MerID"].ToString());
                        m.MerName = item["MerName"].ToString();
                        m.DishesTypeID = item["DishesTypeID"].ToString();
                        m.DishesEnableInt = int.Parse(item["DishesEnableInt"].ToString());
                        m.DishesIsInt = int.Parse(item["DishesIsInt"].ToString());
                        m.DishesMon = double.Parse(DataHelper.MoneyToYuang(item["DishesMon"].ToString()));
                        m.DelPackMon = double.Parse(DataHelper.MoneyToYuang(item["DelPackMon"].ToString()));
                        m.DishesPay = double.Parse(DataHelper.MoneyToYuang(item["DishesPay"].ToString()));
                        m.DishesVol = int.Parse(item["DishesVol"].ToString());
                        m.DishesTxt = item["DishesTxt"].ToString();
                        //m.DishesPic =siteUrl+ item["DishesPic"].ToString();
                        m.DishesPic = item["DishesPic"].ToString();
                        m.DishesPicSmall= item["DishesPicSmall"].ToString();
                        m.DishesEvaNun = int.Parse(item["DishesEvaNun"].ToString());
                        m.DishesEvaTxt = int.Parse(item["DishesEvaTxt"].ToString());
                        m.DishesEvaX = int.Parse(item["DishesEvaX"].ToString());
                        m.DishesGood = int.Parse(item["DishesGood"].ToString());
                        m.DishesTypeState = int.Parse(item["DishesTypeState"].ToString());
                        m.IntBZ = item["IntBZ"].ToString();
                        m.DishesTypeName = item["DishesTypeName"].ToString();
                        list.Add(m);
                    }
                }
                Session[SessionStorage.DisheList] = list;//将实体类保存至Session
                return list;
            }
            else
            {
                return null;
            }
        }

    }
}
